from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 创建 Beautiful Soup 对象
soup = BeautifulSoup(html, features="lxml")

# 通过选择器找
# 根据标签找
print(soup.select('a'))  # 搜索所有的a标签
# 根据类找
print(soup.select('.sister'))
# 根据id找
print(soup.select('#link2'))
# 根据层级选择器找
print(soup.select("body .sister"))
# 根据标签属性找
print(soup.select("a[class='sister']"))


# 找到标签后，取标签中需要的内容
print(soup.select("a[class='sister']")[0].get_text())
# 找到标签后，取标签中指定的属性值
print("select找到的对象类型是：", type(soup.select("a[class='sister']")[0]))
print(soup.select("a[class='sister']")[0].get("href"))
